package ar.edu.unq.svnpermission.ui.model;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import ar.edu.unq.svnpermission.domain.Group;
import ar.edu.unq.svnpermission.domain.User;
import ar.edu.unq.svnpermission.provider.GroupsDataProvider;
import ar.edu.unq.svnpermission.provider.UsersDataProvider;
import ar.unq.edu.svnpermission.homes.GroupHome;

public class Groups extends UseCase {

	private static final long serialVersionUID = 1L;
	private Group group=new Group();
	private List<User> addedUsers = new ArrayList<User>();
	private List<User> availableUsers = new ArrayList<User>();
	private GroupsDataProvider groupsDataProvider;
	
	
	public Groups()
	{
		this.setAvailableUsersList();
		this.setGroupsDataProvider(new GroupsDataProvider(this.getAll()));
	}
	
	
	private void setAvailableUsersList()
	{
		for(User user : new Members().getAllUsers())
		{
			if(this.isAvailable(user))
				this.getAvailableUsers().add(user);
		}
	}
	
	private Boolean isAvailable(User user)
	{
		for(User item : this.getAddedUsers())
		{
			if(user.getEmail().equals(item.getEmail()))
				return false;
		}
		return true;
	}
	
	public void create()
	{
		this.getHome().create(this.getGroup());
	}
	
	public void delete()
	{
		this.getHome().delete(this.getGroup());
	}
	
	public void delete(Group group)
	{
		this.getHome().delete(group);
	}	

	public List<Group> getAll()
	{
		return this.fixList(this.getHome().allInstances());
		
	}
	
	public List<Group> getAll(User user)
	{
		List<Group> returnGroups = new ArrayList<Group>();
		for(Group group : this.fixList(this.getHome().allInstances()))
		{
			for(User userItem: group.getUsers())
			{
				if(userItem!=null && userItem.getEmail().equals(user.getEmail()))
					returnGroups.add(group);
			}
		}
			
		return returnGroups;
		
	}	
	
	/**
	 * Se utiliza para remover de la lista las instancias invalidad. Perdon, no me quedo otra!
	 */
	private  List<Group> fixList(List<Group> groups)
	{
		
		List<Group> validGroups = new ArrayList<Group>();
		Iterator<Group> it = groups.iterator();
		
		//FIXME
		while(it.hasNext())
		{
			Object item=it.next(); 
			
			if(item.getClass()==Group.class)
			{
				validGroups.add((Group)item);
			}
		}
		
		return validGroups;
	}	

	
	private GroupHome getHome()
	{
		return this.getHome(Group.class, GroupHome.class);
	}	
	

	public Group getGroup() {
		return group;
	}

	public void setGroup(Group group) {
		this.group = group;
		this.getAddedUsers().addAll(group.getUsers());
	}
	
	public UsersDataProvider getAvailableUsersDataProvider()
	{

		return new UsersDataProvider(this.getAvailableUsers());
	}
	
	public void addToAddedUsers(User user)
	{
		this.getAddedUsers().add(user);
		this.getAvailableUsers().remove(user);		
		this.getGroup().addUser(user);
	}
	
	public void addToAvailableUsers(User user)
	{
		this.getAvailableUsers().add(user);
		this.getAddedUsers().remove(user);
		this.getGroup().removeUser(user);
	}
	
	public UsersDataProvider getAddedUsersDataProvider()
	{
		return new UsersDataProvider(this.getAddedUsers());
	}	
	
	public GroupsDataProvider getGroupsDataProvider()
	{
		return groupsDataProvider;
	}
	
	public void setGroupsDataProvider(GroupsDataProvider dataProvider)
	{
		groupsDataProvider = dataProvider;
	}	
	
	public void reSetGroupsDataProvider()
	{
		this.setGroupsDataProvider(new GroupsDataProvider(this.getAll()));
	}	
		
	
	public String getKeyName()
	{
		return this.getGroup().getKeyName();
	}
	
	public void setKeyName(String name)
	{
		this.getGroup().setKeyName(name);
	}

	public List<User> getAddedUsers() {
		return addedUsers;
	}

	public void setAddedUsers(List<User> unavailableUsers) {
		this.addedUsers = unavailableUsers;
		this.setAvailableUsersList();
	}

	public List<User> getAvailableUsers() {
		return availableUsers;
	}

	public void setAvailableUsers(List<User> availableUsers) {
		this.availableUsers = availableUsers;
	}

}
